﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NetMoveCwS
{
    public partial class Form1 : Form
    {
        NetServer NS = new NetServer();
        string IpAddr;


        public Form1()
        {
 

            //NS.Start();
            InitializeComponent();

            NS.LoadIPv4();
            for (int i = 0; i < NS.GetIPv4Count(); i++)
            {
                string s = NS.GetIPv4(i);

                listBox1.Items.Add(s);
            }
            listBox1.SetSelected(0, true);

            IpAddr = listBox1.Items[listBox1.SelectedIndex].ToString();

            

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            IpAddr = listBox1.Items[listBox1.SelectedIndex].ToString();


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            NS.Start(IpAddr);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = string.Format("Players connected: {0}", (NS.PlayersActiveCount));
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label2.Text = "";
            label3.Text = "";
            if (NS.Started) 
            {
                NS.ReceiveFromAll();
                NS.SendForAll();

                for (int i=0; i<NS.PlayersActiveCount; i++) 
                {
                    label2.Text += String.Format("id {0}", NS.PlayersID[i]) + Environment.NewLine;

                    int x = NS.PlayersSendData[i].x;
                    int y = NS.PlayersSendData[i].y;

                    label3.Text += String.Format("x = {0} y = {1}", x, y) + Environment.NewLine;

                }
            }
        }
    }
}